using AutoMapper;
using Interface;
using Microsoft.Extensions.Logging;
using Repository;
using SqlSugar;

namespace Service;

/// <summary>
/// 基础服务实现类
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <typeparam name="TVo">视图对象类型</typeparam>
/// <remarks>
/// 提供通用的CRUD业务逻辑实现，自动处理实体与VO之间的映射
/// 使用AutoMapper进行对象映射，使用ILogger记录日志
/// </remarks>
public class BaseService<TEntity, TVo> : IBaseService<TEntity, TVo> 
    where TEntity : class, new()
    where TVo : class, new()
{
    private readonly ISqlSugarClient _db;
    private readonly IMapper _mapper;
    private readonly ILogger _logger;
    private readonly BaseRepository<TEntity> _repository;
    
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="db">SqlSugar数据库客户端</param>
    /// <param name="repository">实体仓储</param>
    /// <param name="mapper">AutoMapper映射器</param>
    /// <param name="logger">日志记录器</param>
    public BaseService(ISqlSugarClient db, BaseRepository<TEntity> repository, IMapper mapper, ILogger<BaseService<TEntity, TVo>> logger)
    {
        _db = db;
        _repository = repository;
        _mapper = mapper;
        _logger = logger;
    }
    
    /// <summary>
    /// 获取所有记录（返回VO）
    /// </summary>
    /// <returns>VO列表</returns>
    public async Task<List<TVo>> GetAllAsync()
    {
        var entities = await _repository.GetAllAsync();
        return _mapper.Map<List<TVo>>(entities);
    }

    /// <summary>
    /// 根据ID获取单条记录（返回VO）
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns>VO对象，不存在则返回null</returns>
    public async Task<TVo> GetByIdAsync(object id)
    {
        var entity = await _repository.GetByIdAsync(id);
        return _mapper.Map<TVo>(entity);
    }

    /// <summary>
    /// 分页查询记录（返回VO）
    /// </summary>
    /// <param name="page">页码，从1开始</param>
    /// <param name="pageSize">每页数量</param>
    /// <returns>VO列表</returns>
    public async Task<List<TVo>> GetPageAsync(int page, int pageSize)
    {
        var query = _db.Queryable<TEntity>();
        var entities = await query
            .OrderBy("Id")
            .Skip((page - 1) * pageSize)
            .Take(pageSize)
            .ToListAsync();
        return _mapper.Map<List<TVo>>(entities);
    }

    /// <summary>
    /// 添加单条记录
    /// </summary>
    /// <param name="entity">要添加的实体</param>
    /// <returns>是否添加成功</returns>
    public async Task<bool> AddAsync(TEntity entity)
    {
        var result = await _repository.AddAsync(entity);
        return result != null;
    }

    /// <summary>
    /// 更新记录
    /// </summary>
    /// <param name="entity">要更新的实体</param>
    /// <returns>是否更新成功</returns>
    public async Task<bool> UpdateAsync(TEntity entity)
    {
        return await _repository.UpdateAsync(entity);
    }

    /// <summary>
    /// 根据ID删除记录
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns>是否删除成功</returns>
    public async Task<bool> DeleteByIdAsync(object id)
    {
        return await _repository.DeleteByIdAsync(id);
    }
}